<template>
    <el-main>
      <!-- 查询条件 -->
      <general-query :bbid="bbid" @onEmitSearch="onEmitSearch" @download="download" @saveSQL="saveSQL"></general-query>
      <!-- 数据表格 -->
      <el-table
        :data="tableData"
        border
        stripe
        style="width: 100%; margin-bottom: 10px"
        :header-cell-style="{background:'#F5F7FA',color:'#909399'}"
      >
        <el-table-column 
            v-for="(item, index) in btList" 
            :key="index" 
            :label="item.lmc"
            :prop="item.zdmc"
            align="center">
            <el-table-column 
                v-for="(itemc, index) in item.children" 
                :key="index" 
                :label="itemc.lmc"
                :prop="itemc.zdmc"
                align="center">
                
            </el-table-column>
        </el-table-column>
        <!-- <el-table-column prop="fwdm" label="服务代码" align="center"></el-table-column>
        <el-table-column prop="fwlx" label="服务类型" align="center"></el-table-column>
        <el-table-column prop="fwmc" label="服务名称" align="center" width="600"></el-table-column> -->
      </el-table>
    </el-main>
  </template>
  
  <script>
  import dtbbApi from '@/api/dtbb/bbmb';
  import dtbbShApi from '@/api/dtbb/dtbb';
  import infoApi from "@/api/dtbb/info";
  import GeneralQuery from '../components/GeneralQuery';
  import { getToken } from "@/utils/auth";
 
  export default {
    name: "dtbbList",
    components:{
      GeneralQuery
    },
    data() {
      return {
        // table数据
        tableData: [],
        btList:[],//表头

        bbid:'',

        //查询条件SQL
        nativeSql:'',
      };
    },
    methods: {
      //接收数据
      getinfo(){
        let geturl = window.location.href
        console.log(geturl);
        let listurl = geturl.split('=');
        this.bbid = listurl[1]
        // this.bbid =this.$route.query.bbid;
        console.log(this.bbid);
      },

      /**
      * 查询列表
      */
      async search() {
        //发送查询请求
        let res = await dtbbApi.getBbDataByQuery({bbid:this.bbid});
        //判断是否成功
        if(res.success){
          console.log(res.data);
          //赋值
          this.tableData = res.data;
          //总数量
          this.total = res.data.total;
        }
      },
      /**
     * 查询字段树形列表
     */
     async searchTreeListZd() {
      // this.pageNo = pageNo; //当前页码
      // this.pageSize = pageSize; //每页显示数量
      //发送查询请求
      let res = await infoApi.getTreeList({bbid:this.bbid});
      //判断是否成功
      if (res.success) {
        console.log(res.data);
        //赋值
        this.btList = res.data;
      }
    },
    //接收子组件数据并根据字段条件查询
    async onEmitSearch(formInline,bmc){
      console.log(formInline);
      var name= formInline.label;
      var value= formInline.value;
      var condition= formInline.condition;
      if(condition == 1){
        var str = " and " +bmc+"."+name+" like '%"+value+"'";
      }else if(condition == 2){
        var str = " and " +bmc+"."+name+" like '"+value+"%'";
      }else if(condition == 3){
        var str = " and " +bmc+"."+name+" like '%"+value+"%'";
      }else{
        var str = " and " +bmc+"."+name+" "+condition+" '"+value+"'";
      }
      
      console.log(str);
      let res = await dtbbApi.getBbData({bbid:this.bbid,nativeQuery:str});
      //判断是否成功
      if(res.success){
        console.log(res.data);
        this.$message.success(res.message);
        this.nativeSql = str
        //赋值
        this.tableData = res.data;
        //总数量
        this.total = res.data.total;
      }

    },
    async saveSQL(formInline){
      if(this.nativeSql != ''){
        console.log(this.nativeSql);
        let res = await dtbbShApi.addSQL({id:'',bbid:this.bbid,nativeSql:this.nativeSql});
        //判断是否成功
        if(res.success){
          console.log(res.data);
          this.$message.success(res.message);
          this.search();
        }
      }else{
        this.$message.error('请先查询！');
      }
      
      

    },
    download(){
      console.log(this.bbid);
      // window.open("http://47.108.251.250:3296/api/dtbb/DownloadMyExcel?token=" + getToken() + '&bbid=' + this.bbid);
      window.open("http://47.108.251.250:3296/api/dtbb/DownloadMyExcel?token=" + getToken() + '&bbid=' + this.bbid);
    }
    },
    //初始化时调用
    created() {
      this.getinfo();
    },
    //挂载后调用
    mounted() {
      this.search();
      this.searchTreeListZd();
    },
  };
  </script>
  
  <style lang="scss" scoped>
  
  .tablestyle{
      display: flex;
      justify-content: center;
      align-items: center;
}
</style>
  